  function [inStru,flagsStru]=checkFieldsWithFlags(inStru,inStruName,flagsStru,flagsStruName,defaultValue)
%
%% I. Description 
% [inStru,flagsStru]=checkFieldsWithFlags(inStru,inStruName,flagsStru,flagsStruName,defaultValue)
%
% Check if the structure *inStru* has a non-empty field with name
% *inStruName*. 
%
% Record this into the structure *flagsStru* by creating the field *flagStruName* and assigning a 
% true or false value to it. 
%
% If *defaultValue* is defined (nargin==5), and *flagsStru.(flagsStruName)==0* assign it
% the value in *defaultValue*
%
% *Note* Even if a default value is assigned, flagsStru.(flagsStruName)==0
% if originally not defined or empty 
% 
%% II. Inputs 
% 
% *inStru*      structure 
%
% *inStruName*  string, name of field of InStru that wish to check
%
% *flagsStru*   structure of flags 
%
% *flagsStru*   string, name of field of flagsStru to assign the existance
%               and non-empty indicator
%
% *defaultValue* optional, if defined, will be assigned to
%                inStru.(inStruName)
%
%% III. Output
%
%  *inStru* 
%
%  *flagsStru*
%
%% IV. Related files 
%
% *ch_field*: similar but does not require a flags structure, though it produces one as first output  
%
%% IV. Version  
%
% Alejandro Justiniano (C) Nov 1 2013 
%
% *Revisions* 
% ========================================================================

%% 1. Check Inputs are of the correct form 
if isstruct(inStru)==false 
    error('inStru must be a structure') 
end 
if isstruct(flagsStru)==false 
    error('flagsStru must be a structure') 
end 
if ischar(char(inStruName))==false 
    error('inStruName must be a string') 
end 
if ischar(char(flagsStruName))==false 
    error('flagsStruName must be a string') 
end 

%% 2. Check if field exists and not empty 
fieldExists=0; 
if isfield(inStru,inStruName)==true && isempty(inStru.inStruName)==false 
    fieldExists=0; 
end 
flagsStru.(flagsStruName)=fieldExists; 

%% 3. Assign default value, if any 
if isempty(defaultValue)==false 
    inStru.(inStruName)=defaultValue; 
end 
